﻿using System;
using System.Collections.Generic;
using System.Linq;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Entities;
using WFramework.MvcPager;
using WFramework.ORM;

namespace MiniWeb.Dao
{
    public class AccountsInfoDao : BaseDao<AccountsInfo>
    {
        public PagedList<AccountsInfo> GetPagedList(string account,int pageIndex, int pageSize)
        {
            var sql = Sql.Builder.Where("1=1");
            if(!string.IsNullOrEmpty(account))
            {
                sql.Where("Account = @0", account);
            }
            return PagedList<AccountsInfo>(pageIndex, pageSize, sql);
        }

       public bool ExitsEmail(string email)
       {
           var sql = Sql.Builder.Where("Email = @0", email);
           return FirstOrDefault(sql) != null;
       }
       
        public AccountsInfo GetModelByNickName(string nickname)
        {
            var sql = Sql.Builder.Where("NickName = @0", nickname);
            return FirstOrDefault(sql);
        }
        public AccountsInfo GetById(int id)
        {
            var sql = Sql.Builder.Where("id = @0", id);
            return FirstOrDefault(sql);
        }

        public AccountsInfo GetByAccount(string account)
        {
            var sql = Sql.Builder.Where("Account = @0", account);
            return FirstOrDefault(sql);
        }

        public void Delete(int id)
        {
            var sql = Sql.Builder.Where("ID = @0", id);
            Delete(sql);
        }


        public bool Exists(int id)
        {
            var sql = Sql.Builder.Where("ID = @0", id);
            if (FirstOrDefault(sql) != null)
            {
                return true;
            }
            return false;
        }
        public bool ExistsAccount(string account)
        {
            var sql = Sql.Builder.Where("Account = @0", account);
            if (FirstOrDefault(sql) != null)
            {
                return true;
            }
            return false;
        }


        public bool IsVip(string nickname)
        {
            var sql =
                Sql.Builder.Select("Count(1)").From("AccountsInfo WITH(NOLOCK)").Where(
                    "NickName = @0 AND VipEndTime > @1", nickname, DateTime.Now);
            return FirstOrDefault<int>(sql) > 0;
        }

        public List<AccountsInfo> GetAll(int? top, string strwhere)
        {
            string sqltop = "";
            if (top != null)
            {
                sqltop = "TOP " + top.ToString() + " * ";
            }
            else
            {
                sqltop = " * ";
            }
            var sql = Sql.Builder.Select(sqltop).From(" AccountsInfo WITH(NOLOCK)");
            if (!string.IsNullOrEmpty(strwhere))
            {
                sql.Where(strwhere);
            }
            sql.OrderBy("CreateDate Desc");
            return Query<AccountsInfo>(sql).ToList();
        }

        public AccountsInfo GetModelByUID(string uid, int sourcetype)
        {
            var sql = Sql.Builder.Where("UID = @0 AND RegType = @1", uid, sourcetype);
            return FirstOrDefault(sql);
        }

        public bool IsExiteNickName(string nickname)
        {
            var sql = Sql.Builder.Where("NickName = @0", nickname);
            if (FirstOrDefault(sql) != null)
            {
                return true;
            }
            return false;
        }



    }
}